package com.kujie.access.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kujie.access.domain.po.CustomerManagement;
import com.kujie.access.domain.query.CustomerQuery;
import com.kujie.access.domain.vo.CustomerManagementVO;
import com.kujie.access.service.ICustomerManagementService;
import com.kujie.common.core.domain.R;
import com.kujie.common.encrypt.annotation.ApiEncrypt;
import com.kujie.common.excel.utils.ExcelUtil;
import com.kujie.common.mybatis.core.page.PageQuery;
import com.kujie.common.web.core.BaseController;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 客户管理 api
 *
 * @author ck
 * @date 2025-05-26
 */
@Slf4j
@RestController
@RequestMapping("/customer")
@RequiredArgsConstructor
@ApiEncrypt(response = true)
public class CustomerController extends BaseController {
    private final ICustomerManagementService customerManagementService;

    @GetMapping("/list")
    public R<Page<CustomerManagementVO>> list(CustomerQuery customerQuery, PageQuery pageQuery) {
        return R.ok(customerManagementService.list(customerQuery, pageQuery));
    }

    @GetMapping("/detail")
    public R<CustomerManagementVO> detail(Long id) {
        return R.ok(customerManagementService.detail(id));
    }


    @PostMapping("insertOrUpdate")
    public R<?> insertOrUpdate(@RequestBody CustomerManagement customerManagement) {
        return toAjax(customerManagementService.insertOrUpdate(customerManagement));
    }

    @DeleteMapping("/delete/{ids}")
    public R<?> batchDelete(@PathVariable List<Long> ids) {
        return toAjax(customerManagementService.batchDelete(ids));
    }

    @PostMapping("/batchExport")
    public void batchExport(@RequestBody CustomerQuery customerQuery, HttpServletResponse response) {
        List<CustomerManagementVO> customerManagementVOS = customerManagementService.batchExport(customerQuery, response);
        ExcelUtil.exportExcel(customerManagementVOS, "客户列表", CustomerManagementVO.class, response);
    }

    @PostMapping("/batchImport")
    public R<?> batchImport(@RequestPart("file") MultipartFile file) throws Exception {
        return toAjax(customerManagementService.batchImport(file));
    }

    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(CollectionUtil.newArrayList(), "客户列表导入模板", CustomerManagementVO.class, response);
    }
}
